﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace RanbuOM.Utilities
{
    public static class Utils
    {
        public static T GetCustomAttributeOnlyOrNull<T>(this ICustomAttributeProvider provider, bool inherit)
        {
            var attrs = provider.GetCustomAttributes(typeof(T), inherit);
            if (attrs.Length > 1)
            {
                string message = string.Format(
                    "Entity '{0}' should not have more than one attribute of the same kind '{1}'.",
                    provider,
                    typeof(T).Name);
                throw new InvalidOperationException(message);
            }

            return (T)attrs.FirstOrDefault();
        }

        public static uint Align(this uint source, uint step)
        {
            uint result = source;
            while (result % step != 0)
            {
                result++;
            }

            return result;
        }

        public static ushort Align(this ushort source, ushort step)
        {
            ushort result = source;
            while (result % step != 0)
            {
                result++;
            }

            return result;
        }

        public static byte Align(this byte source, byte step)
        {
            byte result = source;
            while (result % step != 0)
            {
                result++;
            }

            return result;
        }

    }
}
